﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script type="text/javascript" src="../../Content/highlighter/scripts/shCore.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushCSharp.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushXml.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushJScript.js"></script>
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCoreDefault.css" />
    <script type="text/javascript">SyntaxHighlighter.all();</script>
    <title></title>
</head>
<body>
    <pre class="brush:xml">
        &lt;div class="form-group" style="margin-top:10px;">
            &lt;label class="col-sm-3 col-lg-2 control-label" for="BrandId">商品品牌:&lt;/label>
            &lt;div class="col-sm-6 col-lg-4 controls">
                &lt;input type="text" name="BrandId" id="BrandId" class="form-control" value="" maxlength="45" placeholder="请输入名称" required data-fv-notempty-message="名称不允许为空" />
            &lt;/div>
        &lt;/div>
    </pre>
    <pre class="brush:js">
        $('#BrandId').selectPage({
            showField: 'Name',
            keyField: 'Id',
            data: '/brand/select',
            //params : function() {
            //    return {'name':'aa','sex':1};
            //},
            multiple: false,
            //ajax请求后服务端返回的数据格式处理
            //返回的数据里必须包含list（Array）和totalRow（number|string）两个节点
            eAjaxSuccess: function (data) {
                var result;
                if (data) {
                    result = data;
                } else {
                    result = undefined;
                }
                return result;
            }
        });
    </pre>
    <pre class="brush:csharp">
        public IActionResult Select()
        {
            var parameters = GetParameters();
            int pageNumber = parameters["pageNumber"];
            int pageSize = parameters["pageSize"];
            string name = parameters["Name"];
            string searchValue = parameters["q_word[]"];
            string keyword = $"%{searchValue}%";
            int shopId = this.Admin.Id;
            string sql = $" from {nameof(ShopBrand)} where Name like @keyword and shopId={shopId}";
            var con = DbBase.CreateSqlCon;
            var totalRow = con.QuerySingle&lt;int>("select count(*)" + sql, new { keyword });
            string searchKey = parameters["searchValue"];
            IEnumerable&lt;ShopBrand> list = null;
            if (string.IsNullOrEmpty(searchKey))
            {
                if (pageNumber &lt; 1)
                {
                    pageNumber = 1;
                }

                if (pageNumber > ((totalRow + pageSize) / pageSize))
                {
                    pageNumber = (totalRow + pageSize) / pageSize;
                }
                list = con.Query&lt;ShopBrand>($"select {typeof(ShopBrand).GetNamesWithComma()}" +
                                               $" " + sql + $" order by Name asc" +
                                               $" offset {(pageNumber - 1) * pageSize} rows fetch next {pageSize} rows only",
                    new { keyword });
                list = list.Append(new ShopBrand { Id = 0, Name = "未选择", CateIds = "", CateNames = "", IsRecommend = false, LastTime = DateTime.Now, Seq = 0 });
                return JsonEx(new { totalRow, list });
            }
            else
            {
                if (searchKey == "0")
                {
                    list = new ShopBrand[1]
                    {
                        new ShopBrand
                        {
                            Id=0,Name="未选择",CateIds = "",CateNames = "",IsRecommend=false,LastTime=DateTime.Now,Seq=0
                        }
                    };
                }
                else
                {
                    list = con.Query&lt;ShopBrand>(
                        $"select {typeof(ShopBrand).GetNamesWithComma()} from {nameof(ShopBrand)} where Id={searchKey}");
                }
                return JsonEx(new { totalRow, list });
            }
        }
    </pre>
</body>
</html>